var Global = {
	userScript : [],
	section : {
		responseObj : "Response",
		type : [ "Home", "Profile" ],
		current : "",
		getCurrent : function(waitProc){
			if ( this.current ) {
				return( this.current );
			}

			if ( !$.isProc(waitProc) )
				return(false);

			var $me = this;

			Services.call("CurSection", function(retObj){
				$me.current = retObj.section;

				waitProc(retObj.section, retObj);
			});
		},
		getName : function(section) {
			var iPos = section.lastIndexOf("/");

			if ( iPos > 0 )
				section = section.substr(iPos + 1);

			var iPos = section.indexOf(".");

			if ( iPos > 0 )
				section = section.substr(0, iPos);

			return(section);
		},
		set : function(section, waitProc) {
			var $me = this;
			var request = Request.create("ChSection", "section", section);

			Services.invoke(request, function(retObj){
				if ( retObj.data )
					$me.current = retObj.data;

				if ( $.isProc(waitProc) )
					waitProc(retObj);
			});
		},
		show : function(section, fpNotify)
		{
			var request = Request.create("GetView",
				"section", section,
				"captureOutput", 1
			);

			request.send(function(resp){
				if ( resp.html ) {
					$("#mainContent").html(Base64().decode(resp.html));
				}
				if ( resp.header ) {
					$("#mainHeader").html(Base64().decode(resp.header));
				}

				if ( $.isProc(fpNotify) )
					fpNotify();

				Global.loadUserScript(resp);
				Global.section.set(section);

				$("#waitPic").style.display = "none";
			});
		},
		change : function(section, fpNotify) {
			return( Global.section.show(section, fpNotify) );
		}
	},
	loadUserScript : function( $response )
	{
		var $userScript = $response.userScript;
		var $section    = $response.section;
		var $handler    = Global.handler["section" + $section + "Loaded"];

		if ( !$userScript )
			return;

		if (Global.userScript.exists($userScript) == null)
		{
			Global.userScript.push($userScript);
			$include( $userScript, $handler );
		}
		else {
			if ( $.isProc( $handler ) )
				$handler(  );
		}
	}
};
/**
 * Controladores de eventos. Se desencadenarán 
 * cuando se cargue una nueva sección.
 */
Global.handler = {
	sectionAdminLoaded : function($response)
	{
		if ( $window.loaded ) {
			AdminPanel.updateButtons();
		}
		else {
			$.ready(function(){
				AdminPanel.updateButtons();
			});
		}
	}
};

window.Global = Global;
